package cn.itcast.bos.web.action.bc;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.bc.Staff;
import cn.itcast.bos.web.action.base.BaseAction;

@Controller("staffAction")
@Scope("prototype")
@Namespace("/bc")
@ParentPackage("bos")
public class StaffAction extends BaseAction<Staff> {

	@Action(value = "staffAction_ajaxList")
	public String ajaxList() {
		List<Staff> staffs = staffService.findAll();
		getValueStack().push(staffs);
		return SUCCESS;
	}

	@Action(value = "staffAction_addStaff", results = { @Result(name = "addStaff", location = "/WEB-INF/pages/base/staff.jsp") })
	// @RequiresRoles(value = "staff_admin")
	public String addStaff() {
		staffService.saveStaff(model);// 添加 model 没有id 如果修改 model 含有原始旧id
		return "addStaff";
	}

	@Action(value = "staffAction_pageQuery")
	public String pageQuery() {
		Map<String, Object> results = new HashMap<String, Object>();
		// 1: Pageable 接口封装 datagrid 请求 page rows 当前页面和每页记录数
		Pageable pageRequest = new PageRequest(page - 1, rows);
		// 业务将调用spring-data接口实现 完成分页查询 获取分页数据返回值 Page对象
		Page<Staff> pageData = staffService.findAll(pageRequest);
		// Page [{},{},{}] List<T> 实现Itrerable 接口 json 序列化 数组 [{},{}]
		results.put("total", pageData.getTotalElements());// map 封装 easyui datagrid 插件需要数据格式 total 和 rows
		results.put("rows", pageData.getContent());
		getValueStack().push(results);
		return SUCCESS;
	}

	@Action(value = "staffAction_batchDelete")
	public String batchDelete() {
		Map<String, Object> results = new HashMap<String, Object>();
		try {
			String ids = (String) getParameter("ids");
			staffService.batchDelete(ids);
			results.put("flag", true);
		} catch (Exception e) {
			results.put("flag", false);
		}
		getValueStack().push(results);
		return SUCCESS;
	}

}
